<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sugar</title>
</head>

<body>
  <script>

    /* 
      数组数据查找：
        查找数组某一项：
          .find(item => 条件);  
        查找数组某一项索引：
          .findIndex(item => 条件);  
    */
    const cartList = [
      { goods_id: 111, goods_name: 'xxx' },
      { goods_id: 222, goods_name: 'yyy' },
      { goods_id: 333, goods_name: 'zzz' },
    ];

    // 条件   item.goods_id === 111
    const res1 = cartList.find(item => item.goods_id === 111);
    const res2 = cartList.find(item => item.goods_name === 'yyy');
    const res3 = cartList.find(item => item.goods_name === 'qqq');

    console.log(res1);    // {goods_id: 111, goods_name: "xxx"}
    console.log(res2);    // {goods_id: 222, goods_name: "yyy"}
    console.log(res3);    // undefined  不存在

    const res4 = cartList.findIndex(item => item.goods_id === 111);
    const res5 = cartList.findIndex(item => item.goods_name === 'yyy');
    const res6 = cartList.findIndex(item => item.goods_name === 'qqq');

    console.log('cartList', cartList);
    console.log(res4);    // 0
    console.log(res5);    // 1
    console.log(res6);    // -1   不存在

  </script>
</body>

</html>